<?php

namespace app\admin\controller;

use app\admin\validate\SPartnersCategory as SPartnersCategoryValidate;
use app\admin\validate\SPartners as SPartnersValidate;
use app\common\business\SPartners as SPartnersBus;
use app\common\lib\Str;
use app\common\lib\toCamelCase;

class SPartners extends AuthBase
{
    /**************** 行业类别管理 **********************/
    public function partnerCategoryList(){
        $where = [];
        // 获取分页参数
        $page = $this->request->param('pageNum', 1);
        $limit = $this->request->param('pageSize', 10);
        $partnersCategoryName = $this->request->get("partnersCategoryName","");
        if ($partnersCategoryName) {
            $where[] = ['partners_category_name', 'like', '%'.$partnersCategoryName.'%'];
        }
        $where[] = ["del_flag", "=", '0'];
        try {
            $menuData = (new SPartnersBus())->getNormalPartnersCategory($where,'*',$limit,$page);
            $menuData = (new toCamelCase())->toCamelCaseArray($menuData);
        }catch (\Exception $e){
            $menuData = [];
        }
        return show(config("status.success"),"行业类别列表",$menuData);
    }

    public function partnerCategoryNotPageList(){
        $where = [];
        // 获取分页参数
        $partnersCategoryName = $this->request->get("partnersCategoryName","");
        if ($partnersCategoryName) {
            $where[] = ['partners_category_name', 'like', '%'.$partnersCategoryName.'%'];
        }
        $where[] = ["del_flag", "=", '0'];
        try {
            $menuData = (new SPartnersBus())->getNormalPartnersCategoryNotPage($where,'*');
            $menuData = (new toCamelCase())->toCamelCaseArray($menuData);
        }catch (\Exception $e){
            $menuData = [];
        }
        return show(config("status.success"),"行业类别列表",$menuData);
    }

    //行业类别添加
    public function partnerCategoryAdd(){
        if (!$this->request->isPost()){
            return show(config("status.error"),"请求方式错误");
        }
        //参数校验
        $allParams = request()->param();
        $data = (new toCamelCase())->toCamelCaseArray($allParams,'1',2);
        $validate = new SPartnersCategoryValidate();
        if(!$validate->check($data)){
            return show(config("status.error"),$validate->getError());
        }
        try {
            $result = (new SPartnersBus())->addPartnersCategory($data);
        }catch (\Exception $e){
            return show(config('status.error'),$e->getMessage());
        }
        return show(config("status.success"),"新增成功",$result);
    }

    //行业类别修改
    public function partnerCategoryEdit(){
        $allParams = request()->param();
        $data = (new toCamelCase)->toCamelCaseArray($allParams,1,2);
        try {
            $result = (new SPartnersBus())->updatePartnersCategoryById($data['partners_category_id'],$data);
        }catch (\Exception $e){
            return show(config('status.error'),$e->getMessage());
        }
        return show(config("status.success"),"修改成功",$result);
    }

    //删除行业类别
    public function partnerCategoryDel(){
        $category = $this->request->param("category","","trim");
        if(empty($category)){
            return show(config("status.error"),"参数不能为空");
        }
        try {
            $result = (new SPartnersBus())->delPartnersCategoryIds($category);
        }catch (\Exception $e){
            return show(config('status.error'),$e->getMessage());
        }
        return show(config("status.success"),"删除成功",$result);
    }

    //行业类别详情
    public function partnerCategoryDetails(){
        $category = $this->request->param("category","","trim");
        if(empty($category)){
            return show(config("status.error"),"参数不能为空");
        }
        try {
            $configData = (new SPartnersBus())->getPartnersCategoryById($category);
            $configData = (new toCamelCase())->toCamelCaseArray($configData);
        }catch (\Exception $e){
            $configData = [];
        }
        return show(config("status.success"),"行业类别详情",$configData);
    }

    /***************************** 合作商管理 *********************************/
    public function partnerList(){
        $where = [];
        // 获取分页参数
        $page = $this->request->param('pageNum', 1);
        $limit = $this->request->param('pageSize', 10);
        $partnersName = $this->request->get("partnersName","");
        if ($partnersName) {
            $where[] = ['a.partners_name', 'like', '%'.$partnersName.'%'];
        }
        $where[] = ["a.del_flag", "=", '0'];
        try {
            $menuData = (new SPartnersBus())->getNormalPartners($where,'a.*,b.partners_category_name',$limit,$page);
            $menuData = (new toCamelCase())->toCamelCaseArray($menuData);
            if($menuData['data']){
                $regionsData = file_get_contents(public_path()."static/region/region.json");
                $regionsArrData = json_decode($regionsData,true)['data'];
                foreach ($menuData['data'] as $k=>$v){
                    $menuData['data'][$k]['provinceName'] = (new Str())->getRegionNameByPid($v['province'],$regionsArrData);
                    $menuData['data'][$k]['cityName'] = (new Str())->getRegionNameByPid($v['city'],$regionsArrData);
                    $menuData['data'][$k]['countyName'] = (new Str())->getRegionNameByPid($v['county'],$regionsArrData);
                }
            }
        }catch (\Exception $e){
            $menuData = [];
        }
        return show(config("status.success"),"合作商列表",$menuData);
    }

    //合作商添加
    public function partnerAdd(){
        if (!$this->request->isPost()){
            return show(config("status.error"),"请求方式错误");
        }
        //参数校验
        $allParams = request()->param();
        $data = (new toCamelCase())->toCamelCaseArray($allParams,'1',2);
        $validate = new SPartnersValidate();
        if(!$validate->check($data)){
            return show(config("status.error"),$validate->getError());
        }
        try {
            $data['province'] = $data['belonging_area'][0];
            $data['city'] = $data['belonging_area'][1];
            $data['county'] = $data['belonging_area'][2];
            $result = (new SPartnersBus())->addPartners($data);
        }catch (\Exception $e){
            return show(config('status.error'),$e->getMessage());
        }
        return show(config("status.success"),"新增成功",$result);
    }

    //更改合作商状态
    public function changePartnersStatus(){
        $partnersId = $this->request->param("partnersId","","trim");
        $status = $this->request->param("status","","trim");
        // 验证 userId 是否存在
        if (empty($partnersId)) {
            return show(config("status.error"), "合作商ID不能为空");
        }
        if ($status == "") {
            return show(config("status.error"), "状态不能为空");
        }
        try {
            $result = (new SPartnersBus())->changeStatusPartners($partnersId,$status);
        }catch (\Exception $e){
            return show(config('status.error'),$e->getMessage());
        }
        return show(config("status.success"),"操作成功",$result);
    }

    //合作商修改
    public function partnerEdit(){
        $allParams = request()->param();
        $data = (new toCamelCase)->toCamelCaseArray($allParams,1,2);
        try {
            $data['province'] = $data['belonging_area'][0];
            $data['city'] = $data['belonging_area'][1];
            $data['county'] = $data['belonging_area'][2];
            $result = (new SPartnersBus())->updatePartnersById($data['partners_id'],$data);
        }catch (\Exception $e){
            return show(config('status.error'),$e->getMessage());
        }
        return show(config("status.success"),"修改成功",$result);
    }

    //合作商详情
    public function partnerDetails(){
        $partners = $this->request->param("partners","","trim");
        if(empty($partners)){
            return show(config("status.error"),"参数不能为空");
        }
        try {
            $configData = (new SPartnersBus())->getPartnersById($partners);
            $configData = (new toCamelCase())->toCamelCaseArray($configData);
            if($configData){
                $configData['belongingArea'] = [
                    $configData['province'],
                    $configData['city'],
                    $configData['county'],
                ];
            }
        }catch (\Exception $e){
            $configData = [];
        }
        return show(config("status.success"),"合作商详情",$configData);
    }

    //删除合作商
    public function partnerDel(){
        $partners = $this->request->param("partners","","trim");
        if(empty($partners)){
            return show(config("status.error"),"参数不能为空");
        }
        try {
            $result = (new SPartnersBus())->delPartnersIds($partners);
        }catch (\Exception $e){
            return show(config('status.error'),$e->getMessage());
        }
        return show(config("status.success"),"删除成功",$result);
    }
}
